<?php
/**
 * 字体管理模块
 *
 * 字体管理模块controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\FontService\FontService;
use Response;

class FontsController extends Controller
{
    protected $font_service;
    public function __construct(FontService $font_service)
    {
        $this->font_service = $font_service;
    }

    /**
     * 获取所有字体
     * @param Request $request
     * @return mixed
     */
    public function postAllFonts(Request $request){
        $check_data = array(
            'user_id'=>'required|integer|exists:users,user_id'
        );
        $v = $this->font_service->store($request,$check_data);
        if ($v->fails())
        {
            return Response::json($this->font_service->createResult('M13001',(object)array()));
        }
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $result = $this->font_service->getList($user_id,$per_page);
        return Response::json($result);
    }

    /**
     * 下载成功回执
     * @param Request $request
     * @return mixed
     */
    public function postDownload(Request $request){
        $check_data = array(
            'user_id'=>'required|integer|exists:users,user_id',
            'font_id'=>'required|integer|exists:fonts,font_id'
        );
        $v = $this->font_service->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('user_id')){
                $resultMes = $this->font_service->createResult('M13001',(object)array());
            }else{
                $resultMes = $this->font_service->createResult('M13002',(object)array());
            }
            return Response::json($resultMes);
        }
        $user_id = intval($request->input('user_id'));
        $font_id = intval($request->input('font_id'));
        $result = $this->font_service->download($user_id,$font_id);
        if($result['state'] == 'M00000'){
            $font_type = $this->font_service->get_font_by_font_id($font_id);
            $result['result'] = $font_type;
            return Response::json($result);
        }else{
            $result['result'] = array('font_type'=>0);
            return Response::json($result);
        }
    }

    /**
     * 获取已下载的字体
     * @return mixed
     */
    public function postDownloaded(Request $request){
        $check_data = array(
            'user_id'=>'required|integer|exists:users,user_id'
        );
        $v = $this->font_service->store($request,$check_data);
        if ($v->fails())
        {
            return Response::json($this->font_service->createResult('M13001',(object)array()));
        }
        $user_id = $request->input('user_id');
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $result = $this->font_service->getdownloadFonts($user_id,$per_page);
        return Response::json($result);
    }

    /**
     * 卸载字体
     * @param Request $request
     * @return array
     */
    public function postUninstall(Request $request){
        $check_data = array(
            'user_id'=>'required|integer|exists:users,user_id'
        );
        $v = $this->font_service->store($request,$check_data);
        if ($v->fails())
        {
            return Response::json($this->font_service->createResult('M13001',(object)array()));
        }
        $user_id = intval($request->input('user_id'));
        $font_id = intval($request->input('font_id'));
        if(empty($font_id)){
            return Response::json($this->font_service->createResult('M13002',(object)array()));
        }
        $result = $this->font_service->unistall($user_id,$font_id);
        return Response::json($result);
    }

    public function postSearch(Request $request){
        $check_data = array(
            'user_id'=>'required|integer|exists:users,user_id'
        );
        $v = $this->font_service->store($request,$check_data);
        if ($v->fails())
        {
            return Response::json($this->font_service->createResult('M13001',(object)array()));
        }
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $search = trim($request->input('search'));
        $result = $this->font_service->get_search_font($user_id,$search,$per_page);
        return Response::json($result);
    }
}
